package uk.co.droidinactu.sony.prst1.home;

import uk.co.droidinactu.sony.prst1.home.data.DataModel;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;

public final class EditPreferences extends PreferenceActivity {

	public static final String PREF_COLLECTION1_NAME = "Collection1Name";
	public static final String PREF_COLLECTION1_NAME_DEF = "Science Fiction";

	public static final String PREF_COLLECTION2_NAME = "Collection2Name";
	public static final String PREF_COLLECTION2_NAME_DEF = "Thriller";

	public static final String PREF_CURRENT_READ_DURATION = "NumberRecentBooks";
	public static final String PREF_CURRENT_READ_DURATION_DEF = "10";

	public static String getCollectionName1(final Context cntxt) {
		final SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(cntxt);
		final String prefVal = mPrefs.getString(EditPreferences.PREF_COLLECTION1_NAME,
				EditPreferences.PREF_COLLECTION1_NAME_DEF);
		return prefVal;
	}

	public static String getCollectionName2(final Context cntxt) {
		final SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(cntxt);
		final String prefVal = mPrefs.getString(EditPreferences.PREF_COLLECTION2_NAME,
				EditPreferences.PREF_COLLECTION2_NAME_DEF);
		return prefVal;
	}

	public static int getCurrentReadDuration(final Context cntxt) {
		final SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(cntxt);
		final String prefVal = mPrefs.getString(EditPreferences.PREF_CURRENT_READ_DURATION,
				EditPreferences.PREF_CURRENT_READ_DURATION_DEF);
		return Integer.parseInt(prefVal);
	}

	public static void setCollectionName1(final Context cntxt, final String val) {
		final SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(cntxt);
		final SharedPreferences.Editor ed = mPrefs.edit();
		ed.putString(EditPreferences.PREF_COLLECTION1_NAME, val);
		ed.commit();
	}

	public static void setCollectionName2(final Context cntxt, final String val) {
		final SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(cntxt);
		final SharedPreferences.Editor ed = mPrefs.edit();
		ed.putString(EditPreferences.PREF_COLLECTION2_NAME, val);
		ed.commit();
	}

	public static void setCurrentReadDuration(final Context cntxt, final String val) {
		final SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(cntxt);
		final SharedPreferences.Editor ed = mPrefs.edit();
		ed.putString(EditPreferences.PREF_CURRENT_READ_DURATION, val);
		ed.commit();
	}

	private ListPreference collection1Name;
	private ListPreference collection2Name;
	private Preference currentlyReading;

	private DataModel dataModel;
	private final Preference.OnPreferenceChangeListener syncToggle = new Preference.OnPreferenceChangeListener() {
		@Override
		public boolean onPreferenceChange(final Preference preference, final Object newValue) {
			boolean retValue = false;
			if (preference.getKey().equals(EditPreferences.PREF_CURRENT_READ_DURATION)) {
				setCurrentReadDuration(EditPreferences.this, newValue.toString());
				currentlyReading.setSummary(String.format(
						getResources().getString(R.string.pref_number_of_recently_read_summary), newValue));
				retValue = true;
			}
			return retValue;
		}
	};

	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.preferences);
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (dataModel == null) {
			dataModel = new DataModel(this);
		}

		final String[] collectionNames = dataModel.getCollectionNames();

		collection1Name = (ListPreference) findPreference(EditPreferences.PREF_COLLECTION1_NAME);
		collection1Name.setEntries(collectionNames);
		collection1Name.setEntryValues(collectionNames);
		String tmpValStr = getCollectionName1(this);
		collection1Name.setSummary(String.format(
				getResources().getString(R.string.pref_collection1_name_summary), tmpValStr));

		collection2Name = (ListPreference) findPreference(EditPreferences.PREF_COLLECTION2_NAME);
		collection2Name.setEntries(collectionNames);
		collection2Name.setEntryValues(collectionNames);
		tmpValStr = getCollectionName2(this);
		collection2Name.setSummary(String.format(
				getResources().getString(R.string.pref_collection2_name_summary), tmpValStr));

		currentlyReading = findPreference(EditPreferences.PREF_CURRENT_READ_DURATION);
		currentlyReading.setOnPreferenceChangeListener(syncToggle);
		final int tmpValInt = getCurrentReadDuration(this);
		currentlyReading.setSummary(String.format(
				getResources().getString(R.string.pref_number_of_recently_read_summary), tmpValInt));
	}
}
